package com.woniuxy.enjoylife.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.enjoylife.entity.Server_order;
import com.woniuxy.enjoylife.entity.dto.ReturnServerOrderDto;
import com.woniuxy.enjoylife.entity.dto.SelectServerOrderDto;
import com.woniuxy.enjoylife.entity.dto.ServerAndOrderDto;
import com.woniuxy.enjoylife.mapper.Server_orderMapper;
import com.woniuxy.enjoylife.service.Server_orderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.enjoylife.utils.OrderNumberCreator;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author teamEnjoyLife75
 * @since 2021-01-19
 */
@Service
public class Server_orderServiceImpl extends ServiceImpl<Server_orderMapper, Server_order> implements Server_orderService {
    @Resource
    private Server_orderMapper serverOrderMapper;


    //新增服务订单
    @Override
    public boolean insertServerOrder(Server_order order, int uid) {
        order.setSerial(OrderNumberCreator.createServerOrderNum()).setUid(uid);
        return serverOrderMapper.insert(order) == 1;
    }

    //查找服务订单（传参见SelectServerOrderDto类，前台传参对应类中属性名，不用属性的不传参就行）
    @Override
    public PageResult<Server_order> selectServerOrder(SelectServerOrderDto dto) {
        Server_order server_order = new Server_order().setUid(dto.getUid()).setSid(dto.getSid());
        Page<Server_order> page = PageHelper.startPage(dto.getPage(), dto.getSize());
        List<Server_order> serverOrderList = serverOrderMapper.select(server_order);
        PageResult<Server_order> pageResult = new PageResult<>();
        pageResult.setCurrentPage(dto.getPage());
        pageResult.setPageSize(dto.getSize());
        pageResult.setTotalPage(page.getPageNum());
        pageResult.setTotal(page.getTotal());
        pageResult.setData(serverOrderList);
        return pageResult;
    }

    //修改服务订单
    @Override
    public boolean updateServerOrder(Server_order serverOrder) {
        return serverOrderMapper.update(serverOrder) >= 1;
    }

    //删除服务订单
    @Override
    public boolean deleteServerOrder(Server_order serverOrder) {
//        Integer result = serverOrderMapper.selectSingle(serverOrder).getResult();
//        if (result == 1 || result == 2) return false;
        return serverOrderMapper.delete(serverOrder) >= 1;
    }

    @Override
    public PageResult<ReturnServerOrderDto> selectServerOrderDto(SelectServerOrderDto dto) {
        Page<ReturnServerOrderDto> pages = PageHelper.startPage(dto.getPage(), dto.getSize());
        List<ReturnServerOrderDto> returnServerOrderDtoList = serverOrderMapper.selectDto(dto);
        return new PageResult<ReturnServerOrderDto>().setCurrentPage(dto.getPage()).setPageSize(dto.getSize()).setTotalPage(pages.getPageNum()).setTotal(pages.getTotal()).setData(returnServerOrderDtoList);
    }

    @Override
    public List findServerAndOrder(Integer uid) {
        List serverAndOrder = serverOrderMapper.findServerAndOrder(uid);

        return serverAndOrder;
    }


    //商家==============================================================================================
    @Override
    public ServerAndOrderDto serverAndOrderDtoService(String serial) {

        ServerAndOrderDto serverAndOrderDto = serverOrderMapper.serverAndOrderDtoMapper(serial);
//        System.out.println("输出serverAndOrderDtos"+serverAndOrderDtos);



        return serverAndOrderDto;
    }


    /**
     * 用户取消服务订单
     * @param id   订单id
     * @return
     */
    @Override
    public boolean cancleOrder(int id) {
        return serverOrderMapper.cancleOrder(id)>0?true:false;
    }

    /**
     * 用户删除订单
     * @param id
     * @return
     */
    @Override
    public boolean deleteOrder(int id) {
        return serverOrderMapper.deleteOrder(id)>0?true:false;
    }
}
